package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.NewCombinationInfoDao;
import com.JZ.QM.common.dao.ProDetailDao;
import com.JZ.QM.common.entity.*;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;

import com.JZ.QM.common.dao.NewCombinationYieldDao;
import com.JZ.QM.service.NewCombinationYieldService;

import javax.annotation.Resource;

/**
 *
 *
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020-06-14 11:19:36
 */
@Service
public class NewCombinationYieldServiceImpl extends ServiceImpl<NewCombinationYieldDao, NewCombinationYieldEntity> implements NewCombinationYieldService {
    @Resource
    NewCombinationYieldDao newCombinationYieldDao;
    @Resource
    ProDetailDao proDetailDao;
    @Resource
    NewCombinationInfoDao newCombinationInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<NewCombinationYieldEntity> page = this.page(
                new Query<NewCombinationYieldEntity>().getPage(params),
                new QueryWrapper<NewCombinationYieldEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据组合id获取组合收益率
    * @param comBaseId 用户修改后的组合id
     * @return
     */
    @Override
    public HashMap<String, Object> getNewCombinationYields(Long comBaseId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //交易日期数组
        String[] tradeDates;
        //收益信息数组
        BigDecimal[] newCombinationIncomes;
        //组合的索引
        int newCombiantionIndex=0;
        //收益信息与交易日期列表
        HashMap<String,Object> newComResult = new HashMap<>();
        //组合交易的天数
        int size=0;
        //用户修改后的组合信息列表
        ArrayList<NewCombinationYieldEntity> newCombinationYieldEntities;

        queryWrapper.select("new_com_yield_money","trade_date")
                .eq("com_base_id",comBaseId);
        queryWrapper.orderByAsc("trade_date");

        newCombinationYieldEntities= (ArrayList) newCombinationYieldDao.selectList(queryWrapper);
        size=newCombinationYieldEntities.size();
        tradeDates=new String[size];
        newCombinationIncomes=new BigDecimal[size];
        //筛选出组合的收益信息与交易日期
        for (Iterator<NewCombinationYieldEntity> it = newCombinationYieldEntities.iterator(); it.hasNext(); ) {
            NewCombinationYieldEntity iter = it.next();
            newCombinationIncomes[newCombiantionIndex]=iter.getNewComYieldMoney();
            tradeDates[newCombiantionIndex]=iter.getTradeDate();
            newCombiantionIndex++;
        }
        newComResult.put("newComIncome",newCombinationIncomes);
        newComResult.put("newProTradeDate",tradeDates);
        return newComResult;
    }

    /**
     * 根据更新日期跟新用户修改后的组合日收益率
     * @param newCombinationUpdateDate 用户修改后的组合的更新日期
     * @return
     */
    @Override
    public boolean updateCombinationDailyIncomeRadio(String newCombinationUpdateDate) {

        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //组合基本信息列表
        ArrayList<NewCombinationInfoEntity> newCombinationInfoEntities=new ArrayList<>();
        //组合id
        Long newComBaseId=0l;
        //产品详细信息列表
        ArrayList<NewProDetailEntity> newProDetailEntities=new ArrayList();
        //产品详细信息
        NewProDetailEntity newProDetailEntity=new NewProDetailEntity();
        //组合收益率
        BigDecimal newCombinationYield = new BigDecimal(0);
        //产品所占比例
        BigDecimal newShareRadio=new BigDecimal(0);
        //产品日常收益率
        BigDecimal newShareIncomeDetailRadio=new BigDecimal(0);
        //组合收益基本信息
        NewCombinationYieldEntity newCombinationYieldEntity=new NewCombinationYieldEntity();
        //影响的函数
        int affectRow=0;

        queryWrapper.select("new_com_id")
                .eq("is_used",2);
        //获取正在上架的组合信息
        newCombinationInfoEntities= (ArrayList<NewCombinationInfoEntity>) newCombinationInfoDao.selectList(queryWrapper);
        for (Iterator<NewCombinationInfoEntity> it = newCombinationInfoEntities.iterator(); it.hasNext(); ) {
            newComBaseId = it.next().getNewComId();
            //获取组合的详细信息
            newProDetailEntities=proDetailDao.getNewComProDetail(newComBaseId,newCombinationUpdateDate);
            for (Iterator<NewProDetailEntity> iterator = newProDetailEntities.iterator(); iterator.hasNext(); ) {
                newProDetailEntity = iterator.next();
                newShareRadio=newProDetailEntity.getNewComShareRadio();
                newShareIncomeDetailRadio = newProDetailEntity.getProIncomeDailyRadio()==null? new BigDecimal(0):newProDetailEntity.getProIncomeDailyRadio();
                System.out.println("用户修改后的组合"+newShareRadio.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%"+newShareIncomeDetailRadio.toString());
                newCombinationYield=newCombinationYield.add(newShareRadio.multiply(newShareIncomeDetailRadio));
            }
            newCombinationYieldEntity.setComBaseId(newComBaseId);
            newCombinationYieldEntity.setNewComYieldMoney(newCombinationYield);
            newCombinationYieldEntity.setTradeDate(newCombinationUpdateDate);
            affectRow=newCombinationYieldDao.insert(newCombinationYieldEntity);
            if( affectRow ==0){
                return false;
            }
            newCombinationYield=new BigDecimal(0);
        }
        return true;

    }

    /**
     * 获取用户修改后的组合收益前三的交易数据
     * @param comBaseId 组合的基本信息id
     * @param comAverage 组合的日平均收益率
     * @return
     */
    @Override
    public ArrayList  getNewCombinationDailyRadio(Long comBaseId,BigDecimal comAverage,String[] dates) {
        //挑选收益率合格的组合编号条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //组合产品收益条件构造器
        QueryWrapper queryWrapperPro=new QueryWrapper();
        //sql语句
        String sql="select new_com_id from new_combination_info WHERE com_base_id="+comBaseId;
        //用户修改后的组合收益信息列表
        ArrayList<NewCombinationYieldEntity> newCombinationYieldEntities=new ArrayList<>();
        //用户修改后的组合代码
        Long newCombinationId=0l;
        //用户修改后的产品详情信息列表
        ArrayList<NewProDetailEntity> newProDetailEntities=new ArrayList<>();
        //用户修改后的组合收益率相应列表
        ArrayList<NewCombinationYieldEntity> newCombinationYieldEntitiesRadio=new ArrayList<>();
        //产品的详细信息
        NewProDetailEntity newProDetailEntity=new NewProDetailEntity();
        //将bigdecimal转换为字符串
        DecimalFormat df = new DecimalFormat("0.00%");
        //用户修改后的组合名称
        String newCombinationDesc="";
        //用户修改后的组合收益基本信息
        NewCombinationYieldEntity newCombinationYieldEntity=new NewCombinationYieldEntity();
        //组合的交易日期数组
        String[] tradeDate;
        //组合的收益信息数组
        BigDecimal[] incomeRadio;
        //收益日期与交易日期数组索引
        int index=0;
        //用户修改后的组合索引
        int indexCom=0;
        //返回结果集基本信息列表
        ArrayList<HashMap<String,Object>> results=new ArrayList<>();
        //返回结果集基本信息
        HashMap<String,Object> result=new HashMap<>();

        //获取用户修改后的组合的日平均收益率大于原组合平均收益率的前三名
        queryWrapper.select("com_base_id")
                .inSql("com_base_id",sql);
        queryWrapper.groupBy("com_base_id");
        queryWrapper.having("AVG(new_com_yield_money)>{0}",comAverage);
        queryWrapper.last("order by AVG(new_com_yield_money) DESC limit 3");
        //获取对应的用户修改后的组合id
        newCombinationYieldEntities= (ArrayList<NewCombinationYieldEntity>) newCombinationYieldDao.selectList(queryWrapper);
        for (Iterator<NewCombinationYieldEntity> it = newCombinationYieldEntities.iterator(); it.hasNext(); ) {
            //对结果集基本信息与条件构造器进行初始化
            queryWrapperPro.clear();
            result=new HashMap<String,Object>();
            newCombinationId= it.next().getComBaseId();
            //获得组合中的所有产品的基本信息
            newProDetailEntities=proDetailDao.getNewComProDetailName(newCombinationId);
            index=0;
            indexCom++;
            newCombinationDesc="修改"+indexCom+"，";
            for (Iterator<NewProDetailEntity> iterator = newProDetailEntities.iterator(); iterator.hasNext(); ) {
                newProDetailEntity= iterator.next();
                newCombinationDesc+=newProDetailEntity.getProName()+":"+df.format(newProDetailEntity.getNewComShareRadio())+";";
            }
            result.put("comName",newCombinationDesc);
            System.out.println("结果为"+newCombinationId);
            //获取对应的所有收益信息
            queryWrapperPro.select("new_com_yield_money","trade_date")
                    .eq("com_base_id",newCombinationId);
            queryWrapper.orderByAsc("trade_date");
            newCombinationYieldEntitiesRadio= (ArrayList<NewCombinationYieldEntity>) newCombinationYieldDao.selectList(queryWrapperPro);
            //初始化数组
            System.out.println("结果为"+newCombinationYieldEntitiesRadio);
            incomeRadio=new BigDecimal[dates.length];
            //获得交易日期列表和收益列表
            for (Iterator<NewCombinationYieldEntity> iterator = newCombinationYieldEntitiesRadio.iterator(); iterator.hasNext(); ) {
                newCombinationYieldEntity= iterator.next();
                index=get(dates,newCombinationYieldEntity.getTradeDate());
                incomeRadio[index]=newCombinationYieldEntity.getNewComYieldMoney();
            }
            //将对应的组合信息加入到结果集中
            result.put("newProTradeDate",dates);
            result.put("newComIncome",incomeRadio);
            results.add(result);
        }
        return results;

    }

    /**
     *获取数组中某个元素的索引
     * @param date
     * @param key
     * @return
     */
    public int get(String[] date,String key){
        for( int i=0;i<date.length;i++){
            if(date[i].equals(key)){
                return i;
            }
        }
        return -1;
    }

}